<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>申领单发卡</title>
    <style type="text/css">
        .items2 ul{ }
        .items2 ul li{margin-bottom: 10px;border: 1px solid #ffffff;height: 30px;line-height: 30px;padding: 0 5px;}
        .items2 ul li.selected {background-color: #E8F1FF;border: 1px solid #B2D1FF;box-shadow: 2px 2px 0 #E5E5E5;font-weight: bold;}
    </style>
</head>

<body>
<div style="width:98%; margin: 0 auto;">

    <div class="title">
        <h2 class="tac">
            <!--{$card_apply_array.apply_id}--> - 申领单发卡
        </h2>
    </div>

    <div class="panel panel-default">
        <h2 class="panel-heading">申领单信息</h2>
        <div class="items2">
            <table class="form-inline">
                <tbody>
                <tr style="height: 40px;">
                    <td class="tal">客户名称：<input type="text" class="form-control w150" name="customer_name" value="<!--{$card_apply_array.customer_name}-->" readonly="readonly"></td>
                    <td class="tal">申请日期：<input type="text" class="form-control w150" name="apply_date" value="<!--{$card_apply_array.apply_date}-->" readonly="readonly"></td>
                </tr>
                <tr style="height: 40px;">
                    <td class="tal">联 系 人：<input type="text" class="form-control w150" name="linkman" value="<!--{$card_apply_array.linkman}-->" readonly="readonly"></td>
                    <td class="tal">联系方式：<input type="text" class="form-control w150" name="contact_way" value="<!--{$card_apply_array.contact_way}-->" readonly="readonly"></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>


    <!--现金卡信息-->

    <div class=" panel panel-default">
        <h2 class="panel-heading">
            现金卡信息
        </h2>
        <table class="table">
            <thead>
            <tr>

            </tr>
            </thead>
            <thead>
            <tr>
                <th>面值</th>
                <th>规格</th>
                <th>数量</th>
                <th>已发数量</th>
                <th>折扣率</th>
                <th>折后价</th>
                <th>代金卡类型</th>
                <th>备注</th>
            </tr>
            </thead>

            <tbody>
            <!--{foreach from=$card_detail_array item=card_detail}-->
                <tr>
                    <td><!--{$card_detail.face_value}--></td>
                    <td><!--{$card_detail.card_spec}--></td>
                    <td><!--{$card_detail.card_num}--></td>
                    <td><!--{$card_detail.send_card_num}--></td>
                    <td><!--{$card_detail.card_discount}--></td>
                    <td><!--{$card_detail.card_total_amount}--></td>
                    <td><!--{$card_detail.card_type}--></td>
                    <td><!--{$card_detail.remark}--></td>
                </tr>
            <!--{/foreach}-->
            </tbody>

        </table>
    </div>


    <div class=" panel panel-default">
        <h2 class="panel-heading">
            卡号信息
        </h2>
        <div class="panel-body">

            <table class="form-inline">
                <tbody>
                <tr>
                    <td width="300">起始卡号：<input type="text" class="form-control w150" id="card_no_start" placeholder="扫描起始卡号" /></td>
                    <td width="300">结束卡号：<input type="text" class="form-control w150" id="card_no_end" placeholder="扫描结束卡号" /></td>
                    <td width="*"><button type="button" class="btn btn-success" id="allocation">确认分配</button></td>
                </tr>
                </tbody>
            </table>
            <br>
            <br>
            <table class="table">
                <thead>
                <tr>
                    <th> 起始卡号 </th>
                    <th> 结束卡号 </th>
                    <th> 数量 </th>
                    <th> 面值 </th>
                    <th> 规格 </th>
                    <th> 操作 </th>
                </tr>
                </thead>
                <tbody id="card_list">

                </tbody>
                <tbody>
                <tr>
                    <td colspan="6">
                        <button type="button" class="btn btn-primary" onclick="apply.save();">发卡激活</button>&nbsp;&nbsp;
                        <button type="button" class="btn btn-default" onclick="E.closeWindows();">关闭</button>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

</div>


<script type="text/javascript">

    $('#card_no_start').keypress(function(event) {

        if (event.keyCode == 13) {

            var card_no = E.trim($(this).val());

            if (E.isEmpty(card_no)) {
                E.alert('起始卡号不能为空', 1, function() {
                    $('#card_no_start').focus();
                });
                return false;
            }

            if (!E.isDigital(card_no) || (card_no.length != 13 && card_no.length != 15)) {
                E.alert('卡号必须13位或15位的整数', 1, function() {
                    $('#card_no_start').focus();
                });
                return false;
            }

            $('#card_no_end').focus();

        }

    });

    $('#card_no_end').keypress(function(event) {

        if (event.keyCode == 13) {

            var card_no = E.trim($(this).val());
            var card_no_start = E.trim($('#card_no_start').val());

            if (E.isEmpty(card_no)) {
                E.alert('结束卡号不能为空', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            if (!E.isDigital(card_no) || (card_no.length != 13 && card_no.length != 15)) {
                E.alert('卡号必须13位或15位的整数', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            if (card_no < card_no_start) {
                E.alert('结束卡号不能小于开始卡号', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            $('#allocation').trigger('click');

        }

    });

    $('#allocation').click(function() {
        apply.allocation();
    });

    var apply = {

        apply_id: <!--{$card_apply_array.apply_id}-->,

        index: 1,

        card_detail: {},

        allocation: function() {

            var card_no_start = E.trim($('#card_no_start').val());
            var card_no_end = E.trim($('#card_no_end').val());

            if (E.isEmpty(card_no_start)) {
                E.alert('起始卡号不能为空', 1, function() {
                    $('#card_no_start').focus();
                });
                return false;
            }

            if (!E.isDigital(card_no_start) || (card_no_start.length != 13 && card_no_start.length != 15)) {
                E.alert('起始卡号必须13位或15位的整数', 1, function() {
                    $('#card_no_start').focus();
                });
                return false;
            }

            if (E.isEmpty(card_no_end)) {
                E.alert('结束卡号不能为空', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            if (!E.isDigital(card_no_end) || (card_no_end.length != 13 && card_no_end.length != 15)) {
                E.alert('结束卡号必须13位或15位的整数', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            if (card_no_end < card_no_start) {
                E.alert('结束卡号不能小于开始卡号', 1, function() {
                    $('#card_no_end').focus();
                });
                return false;
            }

            E.loadding.open('正在检查现金卡，请稍候...');

            E.ajax_get({
                url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=4',
                data: {
                    apply_id: this.apply_id,
                    card_no_start: card_no_start,
                    card_no_end: card_no_end
                },
                call: function( o ) {

                    E.loadding.close();

                    if (o.code == 200) {

                        var html = '<tr id="card_line_' + apply.index + '">';
                        html += '<td>' + card_no_start + '</td>';
                        html += '<td>' + card_no_end + '</td>';
                        html += '<td>' + o.data.card_num + '</td>';
                        html += '<td>' + o.data.face_value + '</td>';
                        html += '<td>' + o.data.card_spec + '</td>';
                        html += '<td><a href="javascript: void(0);" onclick="apply.del(' + apply.index + ');">删除</a></td>';
                        html += '</tr>';
                        $('#card_list').append(html);
                        apply.card_detail[apply.index] = {
                            card_no_start: card_no_start,
                            card_no_end: card_no_end,
                            face_num: o.data.card_num,
                            face_value_id: o.data.face_value_id,
                            face_value: o.data.face_value
                        };
                        apply.index++;
                        $('#card_no_start').val('').focus();
                        $('#card_no_end').val('');

                    } else {
                        E.alert(o.message);

                    }

                }
            });

        },

        del: function(index) {
            $('#card_line_' + index).remove();
            delete apply.card_detail[index];
        },

        save: function() {

            var count = 0;
            $.each(apply.card_detail, function(k, v) {
                count++;
            });

            if (count == 0) {
                E.alert('请录入代金卡', 1, function() {
                    $('#card_no_start').focus();
                });
            } else {

                E.confirm('您确认要激活录入的现金卡吗？', function() {
                    E.ajax_post({
                        url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=7',
                        data: {
                            apply_id: apply.apply_id,
                            card_detail: apply.card_detail
                        },
                        call: function( o ) {

                            E.loadding.close();

                            if (o.code == 200) {

                                E.opener('cardApplyList.load');
                                E.alert(o.message, 2, function() {
                                    E.closeWindows();
                                });

                            } else {

                                E.alert(o.message);

                            }

                        }
                    });
                });

            }

        }

    };

</script>


</body>
</html>
